<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>矢量单体化编辑(GeoJson叠加) | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,web-icons,bootstrap,bootstrap-checkbox,layer,toastr,haoutil,turf,mars3d,mars3d-widget"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <style>
      .infoview div {
        margin-bottom: 5px;
      }
    </style>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <div class="infoview">
      <div>
        <div class="radio radio-info radio-inline">
          <input type="radio" id="queryContent1" name="queryContent" value="1" checked />
          <label for="queryContent1">编辑模式</label>
        </div>
        <div class="radio radio-info radio-inline">
          <input type="radio" id="queryContent2" name="queryContent" value="2" />
          <label for="queryContent2">预览模式</label>
        </div>
      </div>

      <div id="editView">
        <input type="button" class="btn btn-primary" value="单体化面" onclick="drawPolygon()" />
        <input type="button" class="btn btn-danger" value="清除" onclick="deleteAll()" />

        <input id="btnSave" type="button" class="btn btn-primary" value="保存" />
        <input id="btnImpFile" type="button" class="btn btn-primary" value="打开" />
        <input id="input_plot_file" type="file" accept=".json,.geojson" style="display: none" />
      </div>
    </div>

    <!-- 箭头等标号 外部扩展 -->

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;
      var graphicLayerEdit;
      var geoJsonLayerDTH;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { lat: 43.820826, lng: 125.144526, alt: 679, heading: 333, pitch: -32 },
          },
          control: {
            infoBox: false,
          },
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        //三维模型
        var tilesetLayer = new mars3d.layer.TilesetLayer({
          type: "3dtiles",
          name: "校园",
          url: "//data.mars3d.cn/3dtiles/qx-xuexiao/tileset.json",
          position: { alt: 15.8 },
          maximumScreenSpaceError: 1,
          maximumMemoryUsage: 1024,
        });
        map.addLayer(tilesetLayer);

        //单体化图层
        geoJsonLayerDTH = new mars3d.layer.GeoJsonLayer({
          name: "校园-单体化",
          symbol: {
            type: "polygonP",
            styleOptions: {
              //单体化默认显示样式
              color: "rgba(255, 255, 255, 0.01)",
              clampToGround: true,
              classification: true,
              buffer: 1,
              //单体化鼠标移入或单击后高亮的样式
              highlight: {
                type: mars3d.EventType.click,
                color: "rgba(255,255,0,0.4)",
              },
            },
          },
          popup: [
            { field: "name", name: "学校场所" },
            { field: "sfkf", name: "是否开放" },
            { field: "remark", name: "备注信息" },
          ],
        });
        map.addLayer(geoJsonLayerDTH);

        graphicLayerEdit = new mars3d.layer.GraphicLayer({
          hasEdit: true,
          isAutoEditing: true, //绘制完成后是否自动激活编辑
          symbol: {
            type: "polygon",
            styleOptions: {
              color: "rgba(255, 255, 0, 0.4)",
              clampToGround: true,
            },
          },
        });
        map.addLayer(graphicLayerEdit);

        //绑定事件
        graphicLayerEdit.on(mars3d.EventType.drawCreated, function (e) {
          //创建完成
          startEditing(e.graphic);
        });
        graphicLayerEdit.on(mars3d.EventType.editStart, function (e) {
          startEditing(e.graphic);
        });
        graphicLayerEdit.on(mars3d.EventType.editMovePoint, function (e) {
          startEditing(e.graphic);
        });
        graphicLayerEdit.on(mars3d.EventType.editRemovePoint, function (e) {
          startEditing(e.graphic);
        });
        graphicLayerEdit.on(mars3d.EventType.editStop, function (e) {
          //停止编辑
          stopEditing();
          saveEntity(e.graphic);
        });
        graphicLayerEdit.on(mars3d.EventType.removeGraphic, function (e) {
          deleteEntity(e.graphic);
        });

        $("#btnSave").click(function () {
          var strResult = graphicLayerEdit.toGeoJSON();
          if (strResult == null) {
            layer.msg("当前没有标注任何数据，无需保存！");
            return;
          }
          haoutil.file.downloadFile("单体化.json", JSON.stringify(strResult));
        });

        $("#input_plot_file").change(function (e) {
          var file = this.files[0];

          var fileName = file.name;
          var fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();
          if (fileType != "json") {
            layer.msg("文件类型不合法,请选择json格式标注文件！");
            clearSelectFile();
            return;
          }

          if (window.FileReader) {
            var reader = new FileReader();
            reader.readAsText(file, "UTF-8");
            reader.onloadend = function (e) {
              var json = this.result;
              graphicLayerEdit.loadGeoJSON(json, {
                clear: true,
                flyTo: true,
              });

              clearSelectFile();
            };
          }
        });

        $("#btnImpFile").click(function () {
          $("#input_plot_file").click();
        });

        $('input:radio[name="queryContent"]').change(function () {
          var selectType = $(this).val();
          if (selectType == "1") {
            $("#editView").show();
            toBJMS();
          } else {
            $("#editView").hide();
            toYLMS();
          }
        });

        queryDthData().then(function (result) {
          graphicLayerEdit.loadGeoJSON(result);
        });
      }
      //加载数据
      function queryDthData() {
        return new Promise(function (resolve, reject) {
          $.ajax({
            url: "//data.mars3d.cn/file/geojson/dth-xuexiao-fd.json",
            type: "get",
            dataType: "json",
            success: function (result) {
              resolve(result);
            },
            error: function (data) {
              reject(data);
            },
          });
        });
      }

      //保存方法，可以将数据提交到后台
      function saveEntity(graphic) {
        var geojson = graphicLayerEdit.toGeoJSON(graphic); //将geojson存到数据库
        console.log("geojson", geojson);
      }

      //删除方法，可以将数据提交到后台
      function deleteEntity(graphic) {
        var entityId = graphic.id;
      }

      //切换到预览模式
      function toYLMS() {
        var geojson = graphicLayerEdit.toGeoJSON();

        geoJsonLayerDTH.load({ data: geojson });

        graphicLayerEdit.hasEdit = false;
        graphicLayerEdit.show = false;
      }

      //切换到编辑模式
      function toBJMS() {
        geoJsonLayerDTH.clear();

        graphicLayerEdit.hasEdit = true;
        graphicLayerEdit.show = true;
      }

      function clearSelectFile() {
        if (!window.addEventListener) {
          document.getElementById("input_plot_file").outerHTML += ""; //IE
        } else {
          document.getElementById("input_plot_file").value = ""; //FF
        }
      }

      function deleteAll() {
        graphicLayerEdit.clear();
      }

      function drawPolygon(clampToGround) {
        graphicLayerEdit.startDraw({
          type: "polygon",
          style: {
            color: "#ffff00",
            opacity: 0.3,
            clampToGround: true,
          },
        });
      }

      //附加：激活属性编辑widget【非必需，可以注释该方法内部代码】
      var timeTik;

      function startEditing(graphic) {
        clearTimeout(timeTik);

        var plotAttr = mars3d.widget.getClass("widgets/plotAttr/widget.js");
        if (plotAttr && plotAttr.isActivate) {
          plotAttr.startEditing(graphic, graphic.coordinates);
        } else {
          mars3d.widget.activate({
            map: map,
            uri: "widgets/plotAttr/widget.js",
            name: "属性编辑",
            graphic: graphic,
            lonlats: graphic.coordinates,
            attrList: [
              { name: "id", label: "主键", type: "hidden", defval: "" },
              { name: "name", label: "学校场所", type: "text", defval: "" },
              { name: "sfkf", label: "是否开放", type: "text", defval: "" },
              { name: "remark", label: "备注信息", type: "textarea", defval: "" },
            ],
          });
        }
      }

      function stopEditing() {
        timeTik = setTimeout(function () {
          mars3d.widget.disable("widgets/plotAttr/widget.js");
        }, 200);
      }
      //附加：激活属性编辑widget【非必需，可以注释该方法内部代码】
    </script>
  </body>
</html>
